<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2018/3/23 0023
 * Time: 上午 11:28
 */

namespace app\index\controller;

use WechatPhpSdk\Wechat as W;
use WechatPhpSdk\Api;
use think\Controller;
require VENDOR_PATH.'wechatphpsdk/autoload.php';
class Wechati extends Controller {
    public function index()
    {
        $wechat = new W(array(
            // 开发者中心-配置项-AppID(应用ID)
            'appId' 		=>	'wx49cfc16a07e40442',
            //'appId'=>'wx15d22f7800ad9c77',
            // 开发者中心-配置项-服务器配置-Token(令牌)
            'token' 		=> 	'weixin',
            // 开发者中心-配置项-服务器配置-EncodingAESKey(消息加解密密钥)
            // 可选: 消息加解密方式勾选 兼容模式 或 安全模式 需填写
            'encodingAESKey' =>	'jHUdFW9UryNwyncpjupE4oFnl6vNTVgD2Ah7WQsOOGK'
            //'encodingAESKey'=>'lp7dDcbHCfRhUTftNAcHsVfclUfQyHBb6iwfxFcf4n4'
        ));
// 获取微信消息
        $msg = $wechat->serve();
        if($msg->MsgType=='event')
        {
            //点击自定义菜单回复
            if($msg->Event == 'CLICK' )
            {
                if($msg->EventKey=='click_event1')
                {
                    $wechat->reply(
                        array(
                  'type' => 'image',
                 'media_id' => 'MHdXx9HcyVSdCUsNoH2hYTTH-Y3FFTqMoIEg5ep_LlI'
                    ));
                }elseif ($msg->EventKey=='V1001_TODAY_MUSIC')
                {
                    $wechat->reply(  array(
                        'type' => 'news',
                        'articles' => array(
                            array(
                                'title' => '陕西秦岭云电子商务有限公司简介',                                 //可选
                                'description' => '陕西秦岭云电子商务有限公司简介',                           //可选
                                'picurl' => 'http://p2q8gb9dv.bkt.clouddn.com/program/carousel/1/2018/03/e54d0201803231457529191.jpg?',      //可选
                                'url' => 'http://www.chinlingcloud.com/index/about/show/type_pid/3.html/'                          //可选
                            ))
                    ));
                }
            }else
                {
                    //关注事件
                    $wechat->reply("你敢出现的再晚点吗？ 不过，关注我会长肉肉的哟！");
                }
        }

// 回复微信消息
        if ($msg->MsgType == 'text' && $msg->Content == '你好') {
            $wechat->reply("你也好！");
        }elseif($msg->MsgType=='text' && $msg->Content=='图文')
        {
            $wechat->reply(  array(
               'type' => 'news',
               'articles' => array(
                   array(
                       'title' => '图文消息标题1',                                 //可选
                       'description' => '图文消息描述1',                           //可选
                       'picurl' => 'http://me.diary8.com/data/img/demo1.jpg',      //可选
                       'url' => 'http://www.baidu.com/'                          //可选
                   ))
            ));
        }
        else {
            $wechat->reply("欢迎来到秦岭云农场");
        }
        $api=new Api(
            array(
                'appId'=>'wx49cfc16a07e40442',
                'appSecret'=>'902657db3b77b41da662c396ef85d7b8',
                //'appid'=>'wx15d22f7800ad9c77',
                //'appSecret'=>'8361ccde9175c6d6ad1f0f72cdb2f9b5'
            )
        );
        $api->create_menu('
{
    "button":[
        {   
                    "type":"view",
                    "name":"微店",
                    "url":"https://weidian.com/?userid=1288887087&wfr=wxp_wxh5&ifr=shopdetail&share_relation=fda6534c8dbd008c_1292255853_1"
        },
        {
            "name":"福利互动",
            "sub_button":[
                {
                    "type":"click",
                    "name":"玩游戏赢大奖",
                    "key":"click_event1"
                },
                {
                    "type":"view",
                    "name":"秦岭云商场",
                    "url":"http://admin.chinlingcloud.com/"
                }
                
            ]
       },
       {
            "name":"了解我们",
            "sub_button":[
                {
                    "type":"click",
                    "name":"公司简介",
                    "key":"V1001_TODAY_MUSIC"
                }
            ]
       }
    ]
}');
    }

}